$(function(){
	var depCode = ''; //科室代码
	var depName = ''; //科室名称
	var depGroupSum=[]; //科室组汇总
	var depGroupDepR=[]; //科室组科室汇总
	var depsName;
	//页面加上查询条件
	showCondition(true);
	// 获取当前登录的科室和科室名
	var depCode=$("#deps_id").val();
	$("#" + tabId + " #depCode").val(depCode);
	paramTabPage[tabId].depCode = depCode;
	//获取当前登录的科室组汇总信息
	$.ajax({
		url: "DepCliMonitor/getDepGroupSum.dhc",
		data: {
			'pardepCode' : depCode 
		},
		async : false,
		type : "POST",
		success : function(result) {
			if(result.success&& result.data){
				var depNames=[];
				depGroupDepR=result.data;
				depGroupSum = array_uniquelize(depGroupDepR, "CODE");
				for(var i in depGroupDepR){
					depNames.push(depGroupDepR[i].DEP_NAME);
					depsName = depNames.join(",") ;
				}
			}
		}
	});
	
	if(depCode==null||depCode==""){
		//如果角色还未授权科室 无法查看数据
		$('#'+tabId+' #warninginfo').css('display','block');
		$('#'+tabId+' .container').css('display','none');
		return;
	}
	if(depCode.search(/,/)>-1){//管辖多个科室
		if(commonLayerConfig[0]=="1"){
			var j=0;
			for(i=0;i<depGroupSum.length;i++){
				j=i;
				var str = '';
				if(i==0){
					$("#" + tabId + " #depGroupCliTarget_0").html("");
					$("#" + tabId + " #depGroupCliTarget_0").html("<i class='fa fa-file-text-o'></i>"+depGroupSum[0].DECC+"指标");
					$("#" + tabId + " #depGroupCliTarget_0").addClass("drgtips");
					$("#" + tabId + " #depGroupCliTarget_0").attr("tips","所辖授权科室："+findDepByDepGroupCode(depGroupDepR,depGroupSum[0].CODE));
					drgUI.tips();
					getQuotas("departmentCliMonitor_0", loadData_three, "depGroupCli-ul_0" );
				}else{
					str += '<div class="col-xs-12" id="depGroupCliSum_'+i+'"> ';
					str += '<div class="boxcq clearfix">';
					str += '<div class="ti" id="depGroupCliTarget_'+i+'"><i class="fa fa-file-text-o"></i>科室组指标</div> ';
					str += '<ul class="nav nav-tabs boxnavcq" role="tablist" id="depGroupCli-ul_'+i+'"></ul> ';
					str += '<div class="clearfix"></div>';
					str += '<div id="depGroupCliQuotasTopId_'+i+'" class="tab-content"></div> </div> </div>';
					$("#"+tabId+" #depGroupCliSum_0").append(str);
					$("#" + tabId + " #depGroupCliTarget_"+i).html("");
					$("#" + tabId + " #depGroupCliTarget_"+i).html("<i class='fa fa-file-text-o'></i>"+depGroupSum[i].DECC+"指标");
					$("#" + tabId + " #depGroupCliTarget_"+i).addClass("drgtips");
					$("#" + tabId + " #depGroupCliTarget_"+i).attr("tips","所辖授权科室："+findDepByDepGroupCode(depGroupDepR,depGroupSum[i].CODE));
					drgUI.tips();
					getQuotas("departmentCliMonitor_0", loadData_three, "depGroupCli-ul_"+i);
				}
				function loadData_three(totalQuota,type){
					//科室组 (1.depGroup 2.All)
					if(type=="all"){
						for(k=0;k<depGroupSum.length;k++){
							paramTabPage[tabId].depGroupCode=depGroupSum[k].CODE;
							paramTabPage[tabId].depCode='';
							paramTabPage[tabId].deps='';
							loadDepartmentAllQuota("depGroup",totalQuota,"depGroupCliQuotasTopId_"+k,"depGroupCli-ul_"+k,depGroupSum[k].CODE);
						}
						paramTabPage[tabId].depGroupCode='';
						paramTabPage[tabId].depCode='';
						paramTabPage[tabId].deps=depCode;
						//科室组 (1.depGroup 2.All)
						loadDepartmentAllQuota("All",totalQuota,"depGroupCliQuotasTopId_all","depGroupCli-ul_all");
					}else{
						paramTabPage[tabId].depGroupCode=depGroupSum[j].CODE;
						paramTabPage[tabId].depCode='';
						paramTabPage[tabId].deps='';
						loadDepartmentAllQuota("depGroup",totalQuota,"depGroupCliQuotasTopId_"+j,"depGroupCli-ul_"+j,depGroupSum[j].CODE);
					}
				}
			}
			$("#"+tabId+" #depGroupCliSum_00").css("display","block");
		}else if(commonLayerConfig[0]=="0" && commonLayerConfig[1]=="1"){
			$("#"+tabId+" #depGroupCliSum_00").css("display","none");
		}
		  	
			//拼接授权科室汇总指标信息
			var allStr = '';
			allStr += '<div class="col-xs-12" id="depGroupCliSum_all"> ';
			allStr += '<div class="boxcq clearfix">';
			allStr += '<div class="ti" id="depGroupCliTarget_all"><i class="fa fa-file-text-o"></i>授权科室汇总指标</div> ';
			allStr += '<ul class="nav nav-tabs boxnavcq" role="tablist" id="depGroupCli-ul_all"></ul> ';
			allStr += '<div class="clearfix"></div>';
			allStr += '<div id="depGroupCliQuotasTopId_all" class="tab-content"></div> </div> </div>';
			$("#"+tabId+" #depGroupCliSum_0").append(allStr);
			$("#" + tabId + " #depGroupCliTarget_all").addClass("drgtips");
			$("#" + tabId + " #depGroupCliTarget_all").attr("tips","授权科室："+depsName);
			drgUI.tips();
			getQuotas("departmentCliMonitor_0", loadData_four, "depGroupCli-ul_all");
			function loadData_four(totalQuota,type){
				paramTabPage[tabId].depGroupCode='';
				paramTabPage[tabId].depCode='';
				paramTabPage[tabId].deps=depCode;
				paramTabPage[tabId].pardepgroupCode='';
				//加载下面的的div
				//参数：1.查询指标2.表格头部显示的名字3.布局所在的页面4。div下面的ul/
				loadDepartmentQuota(totalQuota,"科室","depCliListQuotasId","depCliList-ul");					 
				//为下半部分加载数据 
				loadDepartmentQuotaData(totalQuota,"dep",false);//科室线  没有交叉
				
				//科室组 (1.depGroup 2.All)
				loadDepartmentAllQuota("All",totalQuota,"depGroupCliQuotasTopId_all","depGroupCli-ul_all");
				
				 
			}
	}else{//管辖一个科室
		paramTabPage[tabId].depCode = '';
		paramTabPage[tabId].deps='';
		//如果所在的科室角色没有配置科室组,指标配置按钮在科室指标上展示
		if(depGroupSum[0].CODE==null||depGroupSum[0].CODE==undefined||depGroupSum[0].CODE==""){
			getQuotas("departmentCliMonitor_0", loadData_one, "depCliList-ul");
		}else{
			getQuotas("departmentCliMonitor_0", loadData_one, "depGroupCli-ul_0");
		}
		function loadData_one(totalQuota,type){
			//如果所在的科室角色没有配置科室组，则不展示科室汇总信息
			if(depGroupSum[0].CODE==null||depGroupSum[0].CODE==undefined||depGroupSum[0].CODE==""){
				$("#"+tabId+" #depGroupCliSum_00").css("display","none");
			}else{
				$("#" + tabId + " #depGroupCode").val(depGroupSum[0].CODE);
				$("#" + tabId + " #depGroupCliTarget_0").html("");
				$("#" + tabId + " #depGroupCliTarget_0").html(depGroupSum[0].DECC+"指标");
				paramTabPage[tabId].depGroupCode = depGroupSum[0].CODE;
				//科室组 (1.depGroup 2.All) 
				loadDepartmentAllQuota("depGroup",totalQuota,"depGroupCliQuotasTopId_0","depGroupCli-ul_0");
			}
			paramTabPage[tabId].depCode=depCode;
			//加载下面的的div
			//参数：1.查询指标2.表格头部显示的名字3.布局所在的页面4。div下面的ul/
			loadDepartmentQuota(totalQuota,"科室","depCliListQuotasId","depCliList-ul");					 
			//为下半部分加载数据 
			loadDepartmentQuotaData(totalQuota,"dep",false);//科室线  没有交叉
		}
	}
	
	/**
	 * 查询科室组所辖科室
	 */
	function findDepByDepGroupCode(dicData,depGroupCode){
		var result = [] ;
		var depName = [] ;
		if(depGroupCode){
			result = array_filter(dicData, "CODE", [depGroupCode]) ;
		}
		for(var o in result) {
			depName.push(result[o].DEP_NAME) ;
		}
		depName = depName.join(",") ;
		return depName ;
	}
	
});

function loadDepToDrg(code,desc){
	var param = jQuery.extend(true,{},paramTabPage[tabId]);
	param.groupName = code;
	openTab('/pages/department/departmentCliDrgMonitor.jsp',desc+'|DRGs','depToDrg'+code,'depToDrg'+code,param);

}